package router

import (
	"fmt"
	"server/app/api/controller"
	"server/app/common"

	beego "github.com/beego/beego/v2/server/web"
	"github.com/beego/beego/v2/server/web/context"
)

func init() {
	ns := beego.NewNamespace("/api",
		beego.NSNamespace("/user",
			beego.NSCtrlGet("/createToken", (*controller.UserController).CreateToken),
		),
	)
	ns.Filter("before", func(ctx *context.Context) {
		if ctx.Input.Header("token") == "" {
			ctx.Output.JSON(common.Response{
				Code: -1,
				Msg:  "token校验失败",
				Data: []string{},
			}, true, false)
		}
	})
	beego.AddNamespace(ns)
	beego.ErrorController(&controller.ErrorController{})
	beego.BConfig.RecoverFunc = func(context *context.Context, config *beego.Config) {
		if err := recover(); err != nil {
			context.Output.JSON(common.Response{
				Code: 0,
				Msg:  fmt.Sprintf("%v", err),
				Data: []string{},
			}, true, false)
		}
	}
}
